//package com.gxa.filter;
//
//import com.gxa.utils.ResponseUtils;
//import com.gxa.utils.ResultCode;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.util.Arrays;
//import java.util.List;
//import java.util.Map;
//
///**
// * Created by shelei on 2022/4/8 14:36
// */
////@Component
//@Slf4j
//public class AuthGlobalFilter implements GlobalFilter, Ordered {
//
//    private static List<String> COMMON_PATHS= Arrays.asList("api-docs");
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
//        ServerHttpRequest request = exchange.getRequest();
//
//        String path = request.getPath().value();
//
//        //判断是否是匿名访问资源
//        for(int i=0;i<COMMON_PATHS.size();i++){
//            if(path.contains(COMMON_PATHS.get(i))){
//                log.info("匿名访问资源:{}",path);
//                return chain.filter(exchange);
//            }
//        }
//        //登录操作
//        if(path.contains("/admin/login")){
//            log.info("登录操作直接放行...");
//            return chain.filter(exchange);
//        }
//
//        //3.认证
//        String token = request.getHeaders().getFirst("token");
//        if(StringUtils.isEmpty(token)){
//            token=request.getQueryParams().getFirst("token");
//            if(StringUtils.isEmpty(token)){
//                log.error("token不能为空!");
//                ServerHttpResponse response = exchange.getResponse();
//                return ResponseUtils.writeErrorInfo(response, ResultCode.TOKEN_INVALID_OR_EXPIRED);
//            }
//        }
//
//        //4.验签
////        if(!TokenUtils.verify(token)){
////            log.info("token验签失败:{}",token);
////            ServerHttpResponse response = exchange.getResponse();
////            return ResponseUtils.writeErrorInfo(response,ResultCode.TOKEN_INVALID_OR_EXPIRED);
////        }
//
//
////        DecodedJWT decodedJWT = JWT.decode(token);
////        Map<String, Claim> claims = decodedJWT.getClaims();
//
//
//
//        log.debug("当前访问的uri为:{}",path);
//
//        //5.查询用户所拥有的权限  jwt进行处理，认证拦截器已经解析了权限信息并且绑定到了当前线程
//        // /village/list,/village/add,/village/update
////        String permissionUrls = claims.get("permissionUris").asString();
//
//
//        //6.判断当前用户访问的uri是否拥有对应的权限，如果有就放行，没有就拦截
//        // /village/list,/village/add,/village/update
//
//        //7.判断
//        //是否拥有权限
//        boolean isOk=false;
//
////        if(!StringUtils.isEmpty(permissionUrls)){
////
////            String[] permissionArray = permissionUrls.split(",");
////            for(String permissionUri:permissionArray){
////
////                if(path.equals(permissionUri)){
////                    isOk=true;
////                    break;
////                }
////
////            }
////
////        }
//
////            if(!isOk){
////                log.debug("用户没有{}:权限",path);
////                ServerHttpResponse response = exchange.getResponse();
////                return ResponseUtils.writeErrorInfo(response,ResultCode.TOKEN_ACCESS_PERMISSION);
////            }
//
//
//        //向下游进行转发
//        return chain.filter(exchange);
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}